//
//  UIImageView+Animated.m
//  CloudsOutpatient
//
//  Created by chengjinliang on 15/12/29.
//  Copyright © 2015年 HC. All rights reserved.
//

#import "UIImageView+Animated.h"

@interface UIImageView ()

@property (nonatomic, copy) NSString* imageurl;                             // 图片url
@property (nonatomic, assign) BOOL isFileExist;                             // 判断图片文件是否存在
@property (nonatomic, assign) BOOL isAnimation;                             // 是否显示动画
@property (nonatomic, assign) BOOL zoomStatus;

@end

static char kUrlImageViewKey;
static char kPlaceholderImageViewKey;
static char kImageUrlKey;
static char kIsFileExistKey;
static char kIsAnimationKey;
static char kzoomStatusKey;

@implementation UIImageView (Animated)

-(BOOL )isFileExist
{
    BOOL ret = [((NSString* )objc_getAssociatedObject(self, &kIsFileExistKey)) intValue];
    return ret;
}

-(void)setIsFileExist:(BOOL )isExist
{
    objc_setAssociatedObject(self, &kIsFileExistKey, [NSString stringWithFormat:@"%d", isExist], OBJC_ASSOCIATION_COPY);
}

-(BOOL )isAnimation
{
    NSString* ani = ((NSString* )objc_getAssociatedObject(self, &kIsAnimationKey));
    return ani ? [ani intValue] : YES;
}

-(void)setIsAnimation:(BOOL )isAnimation
{
    objc_setAssociatedObject(self, &kIsAnimationKey, [NSString stringWithFormat:@"%d", isAnimation], OBJC_ASSOCIATION_COPY);
}

- (BOOL)zoomStatus
{
    NSString* ani = ((NSString* )objc_getAssociatedObject(self, &kzoomStatusKey));
    return ani ? [ani intValue] : NO;
}

- (void)setZoomStatus:(BOOL)zoomStatus
{
    objc_setAssociatedObject(self, &kzoomStatusKey, [NSString stringWithFormat:@"%d", zoomStatus], OBJC_ASSOCIATION_COPY);
}

-(NSString* )imageurl
{
    return objc_getAssociatedObject(self, &kImageUrlKey);
}

-(void)setImageurl:(NSString* )imageurlString
{
    objc_setAssociatedObject(self, &kImageUrlKey, imageurlString, OBJC_ASSOCIATION_COPY);
}


-(UIImageView* )urlImageView
{
    return (UIImageView* )objc_getAssociatedObject(self, &kUrlImageViewKey);
}

-(void)setUrlImageView:(UIImageView *)urlImageView
{
    objc_setAssociatedObject(self, &kUrlImageViewKey, urlImageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIImageView* )placeholderImageView
{
    return (UIImageView* )objc_getAssociatedObject(self, &kPlaceholderImageViewKey);
}

-(void)setPlaceholderImageView:(UIImageView *)placeholderImageView
{
    objc_setAssociatedObject(self, &kPlaceholderImageViewKey, placeholderImageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)createWebCacheSubViewsWithPlaceHolder:(UIImage *)placeholder
{
    if (self.placeholderImageView == nil) {
        UIImageView* placeholder = [((UIImageView* )[UIImageView alloc]) initWithFrame:self.bounds];
        placeholder.clipsToBounds = self.clipsToBounds;
        placeholder.contentMode = self.contentMode;
        self.placeholderImageView = placeholder;
        [self addSubview:self.placeholderImageView];
    }
    
    if (self.urlImageView == nil) {
        UIImageView* urlImage = [((UIImageView* )[UIImageView alloc]) initWithFrame:self.bounds];
        urlImage.clipsToBounds = self.clipsToBounds;
        urlImage.contentMode = self.contentMode;
        self.urlImageView = urlImage;
        [self addSubview:self.urlImageView];
    }
    
    self.placeholderImageView.image = placeholder;
    self.placeholderImageView.hidden = NO;
    self.placeholderImageView.alpha = 1.0f;
    self.urlImageView.alpha = 0.0f;
}
-(void)setImage:(UIImage* )image url:(NSURL *)url {
    if (!IS_NOT_EMPTY([url absoluteString])) {
        return;
    }

    if ([self.imageurl isEqualToString:[url absoluteString]]) {
        
        if (self.zoomStatus) {
            UIImage*scaleImage = [image imageCompressForWidth:self.frameWidth];
            if (scaleImage == nil) {
                self.urlImageView.image = image;
            }else{
                self.urlImageView.image = scaleImage;
            }
        }else{
            self.urlImageView.image = image;
        }
        self.urlImageView.alpha = 1.0f;
    }
    
    if (self.isAnimation && !self.isFileExist) {
        
        CAAnimation *theAnimation2 = [self animationWithOpacityFrom:1.0 To:0.0 Duration:1.1 BeginTime:0];
        [self.placeholderImageView.layer addAnimation:theAnimation2 forKey:nil];
        
        CAAnimation *theAnimation = [self animationWithOpacityFrom:0.0 To:1.0 Duration:1.1 BeginTime:0];
        [self.urlImageView.layer addAnimation:theAnimation forKey:nil];
    }else {
        self.placeholderImageView.hidden = YES;
    }
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    self.placeholderImageView.hidden = YES;
}

- (void)layoutSubviews
{
    self.urlImageView.frame = self.bounds;
    self.placeholderImageView.frame = self.bounds;
}

- (void)setImageUrlWithUrl:(NSURL *)url
{
    self.imageurl = [url absoluteString];
    self.isFileExist = ([[NSFileManager defaultManager] fileExistsAtPath:[[SDImageCache sharedImageCache] defaultCachePathForKey:self.imageurl]]
                        || ([[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:self.imageurl] ? YES : NO));
}

@end
